
package com.shop.cloud.upms.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.upms.admin.service.SysTenantService;
import com.shop.cloud.upms.common.entity.SysTenant;
import com.shop.cloud.upms.common.entity.SysTenantPackage;
import com.shop.cloud.upms.admin.service.SysTenantPackageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 租户套餐
 *
 * @author
 * @date 2023-12-25 11:12:41
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tenantpackage")
@Tag(description = "tenantpackage", name = "租户套餐管理")
public class SysTenantPackageController {

    private final SysTenantPackageService sysTenantPackageService;
    private final SysTenantService sysTenantService;

    /**
     * list查询
     * @param sysTenantPackage
     * @return
     */
    @Operation(summary = "list查询")
    @GetMapping("/list")
    public R getList(SysTenantPackage sysTenantPackage) {
        return R.ok(sysTenantPackageService.list(Wrappers.query(sysTenantPackage)));
    }

    /**
     * 租户套餐分页列表
     * @param page 分页对象
     * @param sysTenantPackage 租户套餐
     * @return
     */
    @Operation(summary = "租户套餐分页列表")
    @GetMapping("/page")
//    @PreAuthorize("@ato.hasAuthority('upms:tenantpackage:index')")
    public R getPage(Page page, SysTenantPackage sysTenantPackage) {
        return R.ok(sysTenantPackageService.page(page, Wrappers.query(sysTenantPackage)));
    }

    /**
     * 租户套餐查询
     * @param id
     * @return R
     */
    @Operation(summary = "租户套餐查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackage:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(sysTenantPackageService.getById(id));
    }

    /**
     * 租户套餐新增
     * @param sysTenantPackage 租户套餐
     * @return R
     */
    @Operation(summary = "租户套餐新增")
    @SysLog("新增租户套餐")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackage:add')")
    public R save(@RequestBody SysTenantPackage sysTenantPackage) {
        return R.ok(sysTenantPackageService.save(sysTenantPackage));
    }

    /**
     * 租户套餐修改
     * @param sysTenantPackage 租户套餐
     * @return R
     */
    @Operation(summary = "租户套餐修改")
    @SysLog("修改租户套餐")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackage:edit')")
    public R updateById(@RequestBody SysTenantPackage sysTenantPackage) {
        return R.ok(sysTenantPackageService.updateById(sysTenantPackage));
    }

    /**
     * 租户套餐删除
     * @param id
     * @return R
     */
    @Operation(summary = "租户套餐删除")
    @SysLog("删除租户套餐")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackage:del')")
    public R removeById(@PathVariable String id) {
       long count =  sysTenantService.count(Wrappers.<SysTenant>lambdaQuery()
                .eq(SysTenant :: getPackageId, id));
       if(count > 0){
           return R.failed("该套餐已有租户使用，不能删除");
       }
       return R.ok(sysTenantPackageService.removeById(id));
    }

    /**
     * 查询当前租户套餐
     * @return R
     */
    @Operation(summary = "查询当前租户套餐")
    @GetMapping("/tenant")
    public R getByTenant() {
        return R.ok(sysTenantPackageService.getById(sysTenantService.getById(TenantContextHolder.getTenantId()).getPackageId()));
    }
}
